package com.wedu.modules.dss.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.R;
import com.wedu.modules.dss.dao.DssDeviceProductionDao;
import com.wedu.modules.dss.dao.DssUsageEfficiencyDao;
import com.wedu.modules.dss.entity.*;
import com.wedu.modules.dss.service.DssFollowService;
import com.wedu.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 * 关注数据表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/dss/follow")
public class DssFollowController  extends AbstractController {
    @Autowired
    private DssUsageEfficiencyDao dssUsageEfficiencyDao;

    @Autowired
    private DssDeviceProductionDao dssDeviceProductionDao;
    @Autowired
    private DssFollowService dssFollowService;
    /**
     * 分页列表
     */
    @RequestMapping("list")
    @RequiresPermissions("dss:follow:list")
    public R list(@RequestParam Map<String,Object> params){
        Long userId = getUserId();//获取当前登录对象对象的Id
        PageUtils page=dssFollowService.queryPage(params,userId);
        QueryWrapper<DssDeviceProductionEntity> productQueryWrapper=new QueryWrapper<>();
        List<DssDeviceProductionEntity> dssDeviceProductionEntities = dssDeviceProductionDao.selectList(productQueryWrapper);
        QueryWrapper<DssUsageEfficiencyEntity> useQueryWrapper=new QueryWrapper<>();
        List<DssUsageEfficiencyEntity> dssUsageEfficiencyEntities = dssUsageEfficiencyDao.selectList(useQueryWrapper);


        R r = R.ok();
        r.put("page", page);
        r.put("production",dssDeviceProductionEntities);
        r.put("efficiency",dssUsageEfficiencyEntities);
        return  r;

    }


    /**
     * 保存(新增关注)
     */
    @RequestMapping("/save")
    @RequiresPermissions("dss:follow:save")
    public R save(@RequestBody String[] deviceIds){
        Long userId = getUserId();//获取当前登录对象对象的Id
        List<DssFollowEntity> followList=new ArrayList<>();
        for (String deviceId : deviceIds) {
            DssFollowEntity dssFollowEntity = new DssFollowEntity();
            dssFollowEntity.setDeviceId(deviceId);
            dssFollowEntity.setUserId(userId);
            followList.add(dssFollowEntity);
        }
        dssFollowService.saveBatch(followList);
        return R.ok();

    }

    /**
     * 删除(取消关注)
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dss:follow:delete")
    public R delete(@RequestBody Long[] ids){
        dssFollowService.removeByIds(Arrays.asList(ids));
       // dssFollowService.remove()
        return R.ok();

    }

}
